package com.usian.article.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.article.mapper.ApArticleMapper;
import com.usian.article.service.ApArticleService;
import com.usian.common.constants.article.ArticleConstants;
import com.usian.model.article.dto.ArticleHomeDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements ApArticleService {
    //每页文章的最大数量
    private final Integer SIZE=50;
    @Resource
    private ApArticleMapper apArticleMapper;
    @Override
    public ResponseResult load(ArticleHomeDto dto, Integer type) {
        //判断参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //限制每页加载的文章数量
        int size = Math.min(dto.getSize(), SIZE);
        dto.setSize(size);
        //判定类型
        if (!type.equals(ArticleConstants.LOADTYPE_LOAD_MORE)&&!type.equals(ArticleConstants.LOADTYPE_LOAD_NEW)){
            type=ArticleConstants.LOADTYPE_LOAD_MORE;
        }
        //判定时间
        if (dto.getMinBehotTime()==null) dto.setMinBehotTime(new Date());
        if (dto.getMaxBehotTime()==null) dto.setMaxBehotTime(new Date());
        //执行查询
        List<ApArticle> list = apArticleMapper.load(dto, type);
        return ResponseResult.okResult(list);
    }
}